﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BIM = Bentley.Interop.MicroStationDGN;
using BMI = Bentley.MstnPlatformNET.InteropServices;
using static Bentley.DgnPlatformNET.CustomProperty;
using 圆管涵正管节;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using System.IO;
using Bentley.MstnPlatformNET;
using Bentley.DgnPlatformNET.DgnEC;
using System.Text.RegularExpressions;
using Bentley.ECObjects.Instance;
using Bentley.GeometryNET;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
using CheccCoverplateCulvertAssemble.Tools;

namespace CheccTexturePropertyGive
{
    public class SetCoverCulvertECProperty
    {
        /// <summary>
        /// 墙身赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="culvertLength">涵台长（m）</param>
        /// <param name="culvertHigh">涵台高（m）</param>
        /// <param name="culvertTopWidth">涵台顶宽（m）</param>
        ///  <param name="culvertBottomWidth">涵台底宽（m）</param>
        /// <param name="bottomChamfer">底部倒角（m）</param>
        ///  <param name="antekliseSlope">台背坡率</param>
        /// <param name="shelveLength">搁置长度（m）</param>
        /// <param name="rebarWeight">钢筋总重</param>
        /// <param name="rebarLength">钢筋总长</param>
        public void SetWallECProperty(List<Element> element, string culvertLength, string culvertHigh, string culvertTopWidth, string culvertBottomWidth, string bottomChamfer, string antekliseSlope, string shelveLength, string rebarWeight = "", string rebarLength = "")
        {
            //涵台长
            string Txt = "culvertLength," + culvertLength;
            //涵台高
            Txt += "\r\n" + "culvertHigh," + culvertHigh;
            //涵台顶宽
            Txt += "\r\n" + "culvertTopWidth," + culvertTopWidth;
            //涵台底宽
            Txt += "\r\n" + "culvertBottomWidth," + culvertBottomWidth;
            //底部倒角
            Txt += "\r\n" + "bottomChamfer," + bottomChamfer;
            //台背坡率
            Txt += "\r\n" + "antekliseSlope," + antekliseSlope;
            //搁置长度
            Txt += "\r\n" + "shelveLength," + shelveLength;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            double volume = 0;
            foreach (var item in element)
            {
                volume += GetVolume(item, myApp);
            }
            Txt += "\r\n" + "volume," + volume;
            //钢筋总重
            Txt += "\r\n" + "rebarWeight," + rebarWeight;
            //钢筋总长
            Txt += "\r\n" + "rebarLength," + rebarLength;
            foreach (var item in element)
            {
                GetCSVFilesPath(item, "盖板涵_墙身_构造体", "墙身", "盖板涵", Txt);
            }
        }

        /// <summary>
        /// 基础赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="basicsAngle">基础角度（°）</param>
        /// <param name="basicsClassify">基础分类</param>
        /// <param name="basicsLength">基础长（m）</param>
        ///  <param name="basicsHigh">基础高（m）</param>
        /// <param name="basicsJB">基础襟边（m）</param>
        /// <param name="rebarWeight">钢筋总重</param>
        /// <param name="rebarLength">钢筋总长</param>
        public void SetBasisECProperty(Element element, string basicsAngle, string basicsClassify, string basicsLength, string basicsHigh, string basicsJB, string rebarWeight = "", string rebarLength = "")
        {
            //基础角度
            string Txt = "basicsAngle," + basicsAngle;
            //基础分类
            Txt += "\r\n" + "basicsClassify," + basicsClassify;
            //基础长
            Txt += "\r\n" + "basicsLength," + basicsLength;
            //基础高
            Txt += "\r\n" + "basicsHigh," + basicsHigh;
            //基础襟边
            Txt += "\r\n" + "basicsJB," + basicsJB;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            Txt += "\r\n" + "volume," + GetVolume(element, myApp);
            //钢筋总重
            Txt += "\r\n" + "rebarWeight," + rebarWeight;
            //钢筋总长
            Txt += "\r\n" + "rebarLength," + rebarLength;
            GetCSVFilesPath(element, "盖板涵_基础_构造体", "基础", "盖板涵", Txt);
        }

        /// <summary>
        /// 帽石赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="capStoneLength">长度（m）</param>
        /// <param name="capStoneHeight">高度（m）</param>
        /// <param name="capStoneWidth">宽度（m）</param>

        public void SetCapStoneECProperty(Element element, string capStoneLength, string capStoneHeight, string capStoneWidth)
        {
            //长度
            string Txt = "capStoneLength," + capStoneLength;
            //高度
            Txt += "\r\n" + "capStoneHeight," + capStoneHeight;
            //宽度
            Txt += "\r\n" + "capStoneWidth," + capStoneWidth;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            Txt += "\r\n" + "capStoneVolume," + GetVolume(element, myApp);
            GetCSVFilesPath(element, "盖板涵_帽石_构造体", "帽石", "盖板涵", Txt);
        }

        /// <summary>
        /// 端墙赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="headWallLength">墙长（m）</param>
        /// <param name="headWallHeight">墙高（m）</param>
        /// <param name="headWallTopWidth">顶宽（m）</param>
        /// <param name="headWallBottomWidth">底宽（m）</param>
        /// <param name="fieldAngle">张角（°）</param>
        /// <param name="wallSlopeRate">墙面坡率</param>
        /// <param name="wallBackSlopeRate">墙背坡率</param>
        public void SetHeadWallECProperty(Element element, string headWallLength, string headWallHeight, string headWallTopWidth, string headWallBottomWidth, string fieldAngle, string wallSlopeRate, string wallBackSlopeRate)
        {
            //长度
            string Txt = "headWallLength," + headWallLength;
            //高度
            Txt += "\r\n" + "headWallHeight," + headWallHeight;
            //顶宽
            Txt += "\r\n" + "headWallWidth," + headWallTopWidth;
            //底宽
            Txt += "\r\n" + "headWallBottomWidth," + headWallBottomWidth;
            //张角
            Txt += "\r\n" + "fieldAngle," + fieldAngle;
            //墙面坡率
            Txt += "\r\n" + "wallSlopeRate," + wallSlopeRate;
            //墙背坡率
            Txt += "\r\n" + "wallBackSlopeRate," + wallBackSlopeRate;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            Txt += "\r\n" + "headWallVolume," + GetVolume(element, myApp);
            GetCSVFilesPath(element, "盖板涵_端墙_构造体", "端墙", "盖板涵", Txt);
        }

        /// <summary>
        /// 端墙基础赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="foundationFacadeFlaperside">基础立面襟边（cm）</param>
        /// <param name="foundationSectionEdge">基础断面襟边（cm）</param>
        /// <param name="foundationThickness">基础厚度（cm）</param>
        /// <param name="foundationLayers">基础层数（cm）</param>
        /// <param name="rebarWeight">钢筋总重</param>
        /// <param name="rebarLength">钢筋总长</param>
        public void SetHeadWallBasisECProperty(Element element, string foundationFacadeFlaperside, string foundationSectionEdge, string foundationThickness, string foundationLayers,string rebarWeight = "", string rebarLength = "")
        {
            //基础立面襟边
            string Txt = "foundationFacadeFlaperside," + foundationFacadeFlaperside;
            //基础断面襟边
            Txt += "\r\n" + "foundationSectionEdge," + foundationSectionEdge;
            //基础厚度
            Txt += "\r\n" + "foundationThickness," + foundationThickness;
            //基础层数
            Txt += "\r\n" + "foundationLayers," + foundationLayers;
            //钢筋总重
            Txt += "\r\n" + "rebarWeight," + rebarWeight;
            //钢筋总长
            Txt += "\r\n" + "rebarLength," + rebarLength;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            Txt += "\r\n" + "headWallBasisVolume," + GetVolume(element, myApp);
            GetCSVFilesPath(element, "盖板涵_端墙基础_构造体", "端墙基础", "盖板涵", Txt);
        }

        /// <summary>
        /// 沉降缝接头赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="settlementJointFillingMaterial">沉降缝填充材料</param>
        /// <param name="settlementJointWrapsAuxiliariesMaterial">沉降缝包裹辅材料</param>
        /// <param name="internalPackingSettlementJoint">沉降缝内部填塞</param>
        /// <param name="settlingSeamUpperProtectiveLayer">沉降缝上部保护层</param>
        /// <param name="settlementSeamWrapWide">沉降缝包裹宽(cm)</param>
        /// <param name="settlementJointWrappedThick">沉降缝包裹厚(cm)</param>
        /// <param name="settlementJointFillingArea">沉降缝填充面积（m2）</param>
        /// <param name="widthStuffingInsideSettlingJoint">沉降缝内部填塞宽度(cm)</param>
        /// <param name="widthUpperProtectiveSettlingSuture">沉降缝上部保护层宽(cm)</param>
        /// <param name="upperProtectiveSettlingSeamThick">沉降缝上部保护层厚</param>
        public void SetSettlingJointECProperty(Element element, string settlementJointFillingMaterial, string settlementJointWrapsAuxiliariesMaterial, string internalPackingSettlementJoint, string settlingSeamUpperProtectiveLayer, string settlementSeamWrapWide, string settlementJointWrappedThick, string settlementJointFillingArea, string widthStuffingInsideSettlingJoint, string widthUpperProtectiveSettlingSuture, string upperProtectiveSettlingSeamThick)
        {
            //沉降缝填充材料
            string Txt = "settlementJointFillingMaterial," + settlementJointFillingMaterial;
            //沉降缝包裹辅材料
            Txt += "\r\n" + "settlementJointWrapsAuxiliariesMaterial," + settlementJointWrapsAuxiliariesMaterial;
            //沉降缝内部填塞
            Txt += "\r\n" + "internalPackingSettlementJoint," + internalPackingSettlementJoint;
            //沉降缝上部保护层
            Txt += "\r\n" + "settlingSeamUpperProtectiveLayer," + settlingSeamUpperProtectiveLayer;
            //沉降缝包裹宽
            Txt += "\r\n" + "settlementSeamWrapWide," + settlementSeamWrapWide;
            //沉降缝包裹厚
            Txt += "\r\n" + "settlementJointWrappedThick," + settlementJointWrappedThick;
            //沉降缝填充面积
            Txt += "\r\n" + "settlementJointFillingArea," + settlementJointFillingArea;
            //沉降缝内部填塞宽度
            Txt += "\r\n" + "widthStuffingInsideSettlingJoint," + widthStuffingInsideSettlingJoint;
            //沉降缝上部保护层宽
            Txt += "\r\n" + "widthUpperProtectiveSettlingSuture," + widthUpperProtectiveSettlingSuture;
            //沉降缝上部保护层厚
            Txt += "\r\n" + "upperProtectiveSettlingSeamThick," + upperProtectiveSettlingSeamThick;
            GetCSVFilesPath(element, "盖板涵_沉降缝接头", "沉降缝接头", "盖板涵", Txt);
        }

        /// <summary>
        /// 换填赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="replacementDepth">换填深度（cm）</param>
        /// <param name="changeTheFlapEdge">换填襟边（cm）</param>
        /// <param name="changeTheFillSlopeRatio">换填坡比</param>
        /// <param name="material">材料</param>
        /// <param name="firstThickeningLength">最后一层边部加厚长度（cm）</param>
        /// <param name="lastThickeningLength">最后一层边部加厚深度（cm）</param>
        public void SetReplacementECProperty(Element element, string replacementDepth, string changeTheFlapEdge, string changeTheFillSlopeRatio, string material, string firstThickeningLength, string lastThickeningLength)
        {
            //换填深度
            string Txt = "replacementDepth," + replacementDepth;
            //换填襟边
            Txt += "\r\n" + "changeTheFlapEdge," + changeTheFlapEdge;
            //换填坡比
            Txt += "\r\n" + "changeTheFillSlopeRatio," + changeTheFillSlopeRatio;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            Txt += "\r\n" + "replacementVolume," + GetVolume(element, myApp);
            //材料
            Txt += "\r\n" + "material," + material;
            //最后一层边部加厚长度
            Txt += "\r\n" + "firstThickeningLength," + firstThickeningLength;
            //最后一层边部加厚深度
            Txt += "\r\n" + "lastThickeningLength," + lastThickeningLength;
            GetCSVFilesPath(element, "盖板涵_换填_构造体", "换填", "盖板涵", Txt);
        }

        /// <summary>
        /// 支撑梁赋EC属性
        /// </summary>
        /// <param name="element"></param>
        /// <param name="supportBeamLength">支撑梁长（m）</param>
        /// <param name="supportBeamWidth">支撑梁宽（m）</param>
        /// <param name="supportBeamheight">支撑梁高（m）</param>
        /// <param name="rebarGrade_HRB400">钢筋等级_HRB400</param>
        /// <param name="rebarWeight_HRB400">钢筋总重_HRB400</param>
        /// <param name="rebarLength_HRB400">钢筋总长_HRB400</param>
        /// <param name="rebarGrade_HPB300">钢筋等级_HPB300</param>
        /// <param name="rebarWeight_HPB300">钢筋总重_HPB300</param>
        /// <param name="rebarLength_HPB300">钢筋总长_HPB300</param>
        public void SetbraceSummerECProperty(Element element, string supportBeamLength, string supportBeamWidth, string supportBeamheight, string rebarGrade_HRB400, string rebarWeight_HRB400, string rebarLength_HRB400, string rebarGrade_HPB300, string rebarWeight_HPB300, string rebarLength_HPB300)
        {
            if (rebarWeight_HRB400.Trim() == "") rebarWeight_HRB400 = 0.ToString();
            if (rebarLength_HRB400.Trim() == "") rebarLength_HRB400 = 0.ToString();
            if (rebarWeight_HPB300.Trim() == "") rebarWeight_HPB300 = 0.ToString();
            if (rebarLength_HPB300.Trim() == "") rebarLength_HPB300 = 0.ToString();
            //支撑梁长
            string Txt = "supportBeamLength," + supportBeamLength;
            //支撑梁宽
            Txt += "\r\n" + "supportBeamWidth," + supportBeamWidth;
            //支撑梁高
            Txt += "\r\n" + "supportBeamheight," + supportBeamheight;
            //体积
            BIM.Application myApp = BMI.Utilities.ComApp;
            Txt += "\r\n" + "supportBeamVolume," + GetVolume(element, myApp);

            if (rebarGrade_HRB400== "HRB400" && rebarGrade_HPB300== "HRB400")
            {
                //钢筋等级_HRB400
                Txt += "\r\n" + "rebarGrade_HRB400," + rebarGrade_HRB400;
                //钢筋总重_HRB400
                Txt += "\r\n" + "rebarWeight_HRB400," + Convert.ToDouble( rebarWeight_HRB400)+ Convert.ToDouble(rebarWeight_HPB300);
                //钢筋总长_HRB400
                Txt += "\r\n" + "rebarLength_HRB400," + Convert.ToDouble(rebarLength_HRB400) + Convert.ToDouble(rebarLength_HPB300);
                //钢筋等级_HPB300
                Txt += "\r\n" + "rebarGrade_HPB300," + "HPB300";
                //钢筋总重_HPB300
                Txt += "\r\n" + "rebarWeight_HPB300," + "";
                //钢筋总长_HPB300
                Txt += "\r\n" + "rebarLength_HPB300," + "";
            }
            else if (rebarGrade_HRB400 == "HRB400" && rebarGrade_HPB300 == "HPB300")
            {
                //钢筋等级_HRB400
                Txt += "\r\n" + "rebarGrade_HRB400," + rebarGrade_HRB400;
                //钢筋总重_HRB400
                Txt += "\r\n" + "rebarWeight_HRB400," + rebarWeight_HRB400;
                //钢筋总长_HRB400
                Txt += "\r\n" + "rebarLength_HRB400," + rebarLength_HRB400;
                //钢筋等级_HPB300
                Txt += "\r\n" + "rebarGrade_HPB300," + rebarGrade_HPB300;
                //钢筋总重_HPB300
                Txt += "\r\n" + "rebarWeight_HPB300," + rebarWeight_HPB300;
                //钢筋总长_HPB300
                Txt += "\r\n" + "rebarLength_HPB300," + rebarLength_HPB300;
            }
            else if (rebarGrade_HRB400 == "HPB300" && rebarGrade_HPB300 == "HRB400")
            {
                //钢筋等级_HRB400
                Txt += "\r\n" + "rebarGrade_HRB400," + "HRB400";
                //钢筋总重_HRB400
                Txt += "\r\n" + "rebarWeight_HRB400," + rebarWeight_HPB300;
                //钢筋总长_HRB400
                Txt += "\r\n" + "rebarLength_HRB400," + rebarLength_HPB300;
                //钢筋等级_HPB300
                Txt += "\r\n" + "rebarGrade_HPB300," + "HPB300";
                //钢筋总重_HPB300
                Txt += "\r\n" + "rebarWeight_HPB300," + rebarWeight_HRB400;
                //钢筋总长_HPB300
                Txt += "\r\n" + "rebarLength_HPB300," + rebarLength_HRB400;
            }
            else
            {
                //钢筋等级_HRB400
                Txt += "\r\n" + "rebarGrade_HRB400," + "HRB400";
                //钢筋总重_HRB400
                Txt += "\r\n" + "rebarWeight_HRB400," + "";
                //钢筋总长_HRB400
                Txt += "\r\n" + "rebarLength_HRB400," + "";
                //钢筋等级_HPB300
                Txt += "\r\n" + "rebarGrade_HPB300," + "HPB300";
                //钢筋总重_HPB300
                Txt += "\r\n" + "rebarWeight_HPB300," +  Convert.ToDouble(rebarWeight_HRB400) +  Convert.ToDouble(rebarWeight_HPB300);
                //钢筋总长_HPB300
                Txt += "\r\n" + "rebarLength_HPB300," +  Convert.ToDouble(rebarLength_HRB400) + Convert.ToDouble(rebarLength_HPB300);
            }

            GetCSVFilesPath(element, "盖板涵_支撑梁_构造体", "支撑梁", "盖板涵", Txt);
        }

        private readonly List<(string standardName, string standValue)> tuples = new List<(string standardName, string standValue)>();

        private void GetCSVFilesPath(Element element, string fileName, string bodyName, string solidName, string TXT)
        {

            List<string> list = SolidHelper.StringSplit(TXT, "\r\n");
            tuples.Clear();
            foreach (string s in list)
            {
                if (s.Contains(","))
                    tuples.Add((s.Split(',')[0], s.Split(',')[1]));
            }
            switch (solidName)
            {
                case "盖板涵":
                    solidName = "SlabCulvertFiles";
                    break;
                case "箱涵":
                    solidName = "BoxCulvertFiles";
                    break;
                case "圆管涵":
                    solidName = "CircularCulvertFiles";
                    break;
                default:
                    break;
            }
            //获取属性文件路径
            string[] strings = { solidName };
            string propertyFileDir = JsonConvert.DeserializeObject<string[]>(SolidHelper.GetINIvalue(strings, ""))[0];
            //属性文件路径
            string path = SolidHelper.WorkSpacePath + propertyFileDir.Substring(1) + "\\" + fileName + ".csv";
            //读取属性文件
            if (!File.Exists(path)) return;
            this.AttachPropertyToEle(path, element, bodyName);
        }

        private void AttachPropertyToEle(string path, Element ele, string bodyName)
        {
            /*
             * 属性名称,计算基准,计算公式或值
               模型元素名称,name,="圆管涵正管节"+ name 
               分类编码,,36-15.03.00.00
               直径,rebarDiameter,=rebarDiameter
               体积,rebarVolume,=rebarVolume
               截面类型,,带肋(46-05.54.03.00)
               钢筋等级,,HRB400(36-15.03.00.00)
               钢筋重量,,=rebarVolume*7.86*1000
               钢筋长度,rebarLength,=rebarLength
             */
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader file = new StreamReader(fs, Encoding.Default);
            _ = file.ReadLine();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string txt;
            while ((txt = file.ReadLine()) != null)
            {
                string[] strings = txt.Split(',');
                dict.Add(strings[0], CalExpressionValue(strings[2], tuples));
            }
            file.Close();
            foreach (var item in dict)
            {
                AttachItemToElement(ele, "盖板涵", bodyName, item.Key, TypeKind.String, item.Value);
            }
        }

        //计算表达式的值  expression 如果是数学表达式 第一个字符是=
        private string CalExpressionValue(string expression, List<(string standardName, string standValue)> tuples)
        {
            if (expression.Contains("="))
            {
                var matchs = Regex.Split(expression.Substring(1), @"\(|\)|\+|\-|\*|\/").Where(s => s != "");
                foreach (var item in matchs)
                {
                    var obj = tuples.FirstOrDefault(p => p.standardName.ToLower() == item.ToLower());
                    if ((null, null) == obj)
                    {
                        continue;
                    }
                    expression = expression.Replace(item, null == obj.standValue ? "" : obj.standValue);
                }
                try
                {
                    string formula = expression.Substring(1);
                    DataTable dt = new DataTable();
                    string really_data = dt.Compute(formula, "false").ToString();
                    double calData = Math.Round(Convert.ToDouble(really_data), 6);
                    return calData.ToString();
                }
                catch
                {
                    string[] strings = expression.Substring(1).Split('+');
                    string really_data = "";
                    if (strings.Count() > 0)
                    {
                        foreach (var item in strings)
                        {
                            really_data += item;
                        }
                    }
                    return really_data;
                }
            }
            else
                return expression;
        }

        //将属性赋值给元素, 参数: 元素ID, 库名, 组名, 属性显示名, 属性类型, 属性值 
        public static void AttachItemToElement(Element elem, string itemLibName, string itemTypeName, string propertyName, TypeKind propertyType, object propertyValue)
        {
            try
            {
                DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
                DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                CustomItemHost host = new CustomItemHost(elem, true);

                ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(itemLibName, dgnFile);
                if (null == itemTypeLibrary)
                {
                    itemTypeLibrary = ItemTypeLibrary.Create(itemLibName, dgnFile);
                }
                ItemType itemType = itemTypeLibrary.GetItemTypeByName(itemTypeName);
                if (itemType == null)
                {
                    itemType = itemTypeLibrary.AddItemType(itemTypeName);
                    itemTypeLibrary.Write();
                }
                itemTypeLibrary.Write();
                IDgnECInstance item = host.GetCustomItem(itemLibName, itemTypeName);
                if (item == null)
                {
                    item = host.ApplyCustomItem(itemType, true);
                }
                CustomProperty property = itemType.GetPropertyByName(propertyName);
                if (property == null)
                {
                    CustomProperty Cp = itemType.AddProperty(propertyName);
                    Cp.Type = propertyType;
                    itemTypeLibrary.Write();
                    switch (Cp.Type)
                    {
                        case TypeKind.Boolean:
                            Cp.DefaultValue = (bool)propertyValue;
                            break;
                        case TypeKind.String:
                            Cp.DefaultValue = (string)propertyValue;
                            break;
                        case TypeKind.DateTime:
                            Cp.DefaultValue = (DateTime)propertyValue;
                            break;
                        case TypeKind.Double:
                            Cp.DefaultValue = (double)propertyValue;
                            break;
                        case TypeKind.Integer:
                            Cp.DefaultValue = (int)propertyValue;
                            break;
                        case TypeKind.Point:
                            Cp.DefaultValue = (DPoint3d)propertyValue;
                            break;
                    }
                }
                else
                {
                    string internalName = property.InternalName;
                    IECPropertyValue val = item.GetPropertyValue(internalName);

                    switch (propertyType)
                    {
                        case TypeKind.Boolean:
                            item.SetValue(internalName, (bool)propertyValue);
                            break;
                        case TypeKind.String:
                            item.SetValue(internalName, (string)propertyValue);
                            break;
                        case TypeKind.DateTime:
                            item.SetValue(internalName, (DateTime)propertyValue);
                            break;
                        case TypeKind.Double:
                            item.SetValue(internalName, (double)propertyValue);
                            break;
                        case TypeKind.Integer:
                            item.SetValue(internalName, (int)propertyValue);
                            break;
                        case TypeKind.Point:
                            item.SetValue(internalName, (DPoint3d)propertyValue);
                            break;
                    }
                }
                itemTypeLibrary.Write();

                item.ScheduleChanges(elem);
                elem.ReplaceInModel(elem);
            }
            catch (Exception)
            {
                return;
            }
        }

        //获取体积 立方米
        public static double GetVolume(Element element, BIM.Application app)
        {
            if (element == null) return 0;
            double ret = 0.0;
            var instCol = DgnECManager.Manager.GetElementProperties(element, ECQueryProcessFlags.SearchAllClasses);
            foreach (var inst in instCol)
            {
                IEnumerator<IECProperty> propertyEnum = inst.ClassDefinition.GetEnumerator();
                while (propertyEnum.MoveNext())
                {
                    if (propertyEnum.Current.Name == "Volume")
                    {
                        IECPropertyValue propertyValue = inst.GetPropertyValue(propertyEnum.Current.Name);
                        ret = propertyValue.DoubleValue / Math.Pow(GeneralBank.UorToMeters, 3);
                        break;
                    }
                }
            }
            return ret;
        }

    }
}
